package com.dbz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dbz.entity.Product;
import com.dbz.entity.Supplier;
import com.dbz.service.ProductService;
import com.dbz.service.SupplierService;
import org.hibernate.validator.constraints.EAN;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/product/")
public class ProductController {
@Resource
private ProductService productService;
@Resource
private SupplierService supplierService;
@RequestMapping("selectProductPage")
    public Map getAllPageByName(String proname,
                                Integer protype,
                                Integer startpronumber ,
                                Integer endpronumber,
                                Integer page,
                                Integer rows){
    //Integer pages = (page - 1) * rows;
    Map<String,Object> map=new HashMap<>();
    QueryWrapper<Product> queryWrapper=new QueryWrapper<Product>();
     Page<Product> pages=new Page<>(page,rows);
     queryWrapper.eq(protype!=null,"protype",protype);
     queryWrapper.like(StringUtils.isNotEmpty(proname),"proname",proname);
    queryWrapper.gt(startpronumber!=null,"pronumber",startpronumber);
    queryWrapper.lt(endpronumber!=null,"pronumber",endpronumber);
     map.put("rows",productService.selectPage(pages,queryWrapper).getRecords());
     map.put("total",productService.selectPage(pages,queryWrapper).getTotal());
     return  map;

}

    /**
     * 添加产品
     * @param product
     * @return
     */
    @RequestMapping("insertProduct")
public Map insertp(Product product) {
        Map<String, Object> map = new HashMap<>();
        String sname = supplierService.selectNameById(product.getProname());
        if (product.getProimg() == "" || product.getProimg() == null) {
            product.setProimg("moren.png");
            List<Product> productList = productService.selectProductGetAll();

            for (Product productList1 : productList) {

                if (sname.equals(productList1.getProname())) {
                    Integer peonumber = product.getPronumber() + productList1.getPronumber();
                    //productService.updateProductNumber(sname, peonumber);
                }
                System.out.println(productList1.getProname() + "1111111111111111");
            }


            return map;

        }
        product.setProname(sname);
        productService.insertProduct(product);
        return map;
    }

    /**
     * 修改产品
     * @param product
     * @return
     */
    @RequestMapping("update")
    public int updateProduct(Product product) {
        productService.updateProductById(product);
        return 1;
    }

    /**
     * 删除产品
     * @param
     * @return
     */
    @RequestMapping("del")
    public int delp(String proid) {
        productService.delproductByid(proid);
        return 1;
    }

    /**
     * 查询供应商
     * @return
     */
    @RequestMapping("selectSupplierAll")
    public  List<Supplier>getAllSupplierList(){
        return supplierService.selectSupplierAll();
    }

    /**ajax单文件上传
     * http://localhost:8080/springmvc04/file/singleFileUpload2
     * @return
     */

    @RequestMapping("singleFileUpload2")
    public Map singleFileUpload2(MultipartFile myfile, HttpServletRequest request,String proid){
        Map map=new HashMap();
        String name=myfile.getName();//表单中name属性值
        String olName=myfile.getOriginalFilename();//获取上传文件的名字
        System.out.println("name="+name+"11111111111111111111111");
        System.out.println("olName="+olName+"222222222222222222222");
        System.out.println(proid+"33333333333333333333333333");
        //获取保存文件的路径
        //String path=request.getServletContext().getRealPath("/file");
        //String path=request.getRealPath("E:\\qfinal\\qfinal\\easyui_ck\\images");
        File file=new File("C:\\Users\\AIERXUAN\\git\\qfinal\\easyui_ck\\images",olName);//创建文件
        productService.updateProductImgById(proid,olName);

        map.put("success", true);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            myfile.transferTo(file);//上传
        }  catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            map.put("success", false);
        }
        return map;
    }
@RequestMapping("queryDeptTreeList")
    public Set<Supplier> suppliergetAll(){
   return  supplierService.queryDeptTreeList();
    }


    /**
     * 根据供应商id查询供应商下的祸
     */
    @RequestMapping("selectDeptChildrenById")
    public Set<Supplier>selectDeptChildrenById(String sid){
     return  supplierService.selectDeptChildrenById(sid);
    }

    @RequestMapping("queryDeptTreeLists")
    public Set<Supplier> suppliergetAlls(){
        return  supplierService.queryDeptTreeLists();
    }

    /**
     * 查询所有商品
     * @return
     */
    @RequestMapping("selectProductGetAll")
      public Set<Product>  selectProductGetAll(){
        return productService.selectProductName();
    }


    /**
     * 销售是修改数量
     * @param proid
     * @param pronumbers
     * @return
     */
    @RequestMapping("updateNumber")
    public Map<String, Object> updateNumber(String proid,Integer pronumbers){
        Map<String, Object> map = new HashMap<String, Object>();
        List<Product> productList=productService.selectById(proid);
        for (Product products:productList){
            if (products.getPronumber()>pronumbers){
            Integer pronumber=products.getPronumber()-pronumbers;
            map.put("up",productService.updateNumber(proid,pronumber));
            return map;
            }else {
                map.put("no","销售数量超过库存");
                return map;
            }
        }
        return map;
    }


}
